import {Component, Input, OnInit, DoCheck} from '@angular/core';
import {Router} from '@angular/router';
import {LocalStorageService} from '../../lib/service/local-storage.service';

@Component({
  selector: 'app-main-side',
  templateUrl: './main-side.component.html',
  styleUrls: ['./main-side.component.scss'],
  providers: [LocalStorageService]
})
export class MainSideComponent implements OnInit, DoCheck {

  sideType = 'store-product/goods-manage';

  operation: string;  // 操作类型 小程序数据管理或者是小程序设置
  goodsManageStatus = true;
  distributionMgmtStatus = false;
  financeMgmtStatus = false;
  userMgmtStatus = false;

  type: string;
  message: string;
  isShow = false;

  @Input() hasAside;

  constructor(private router: Router, private localStorageService: LocalStorageService) {
  }

  ngOnInit() {
  }

  ngDoCheck() {
    if (this.router.url.indexOf('applet-setting') > -1) {
      this.operation = 'setting';
    }
    if (this.router.url.indexOf('store-product') > -1) {
      this.operation = 'management';
    }
    if (this.router.url.indexOf('applet-setting/authorization') > -1) {
      this.sideType = 'applet-setting/authorization';
    }
    if (this.router.url.indexOf('applet-setting/release') > -1) {
      this.sideType = 'applet-setting/release';
    }
    if (this.router.url.indexOf('applet-setting/setting') > -1) {
      this.sideType = 'applet-setting/setting';
    }
    if (this.router.url.indexOf('store-product/goods-manage') > -1) {
      this.sideType = 'store-product/goods-manage';
    }
    if (this.router.url.indexOf('order-manage') > -1) {
      this.sideType = 'store-product/order-manage';
    }
    if (this.router.url.indexOf('delivery-manage') > -1) {
      this.sideType = 'store-product/delivery-manage';
    }
    if (this.router.url.indexOf('temp-manage') > -1) {
      this.sideType = 'store-product/temp-manage';
    }
  }

  changeMenu(menu: string, needChecked?: boolean) {
    if (!this.localStorageService.getNowApplet()) {
      return;
    } else {
      if (needChecked) {
        if (this.localStorageService.getNowApplet().status === '未授权' && !this.localStorageService.getAppletToken()) {
          this.type = 'info';
          this.message = '请先完成小程序授权';
          this.isShow = true;
          return;
        } else {
          this.sideType = menu;
          this.router.navigate(['main/' + menu]);
        }
      } else {
        this.sideType = menu;
        this.router.navigate(['main/' + menu]);
      }
    }
  }

  openNewPage(url) {
    // if (!this.localStorageService.getNowApplet().coupons_key) {
    //   this.type = 'info';
    //   this.message = '请先前往小程序设置，开通卡券功能';
    //   this.isShow = true;
    //   return;
    // }
    window.location.href = url;
  }
}
